package com.atguigu.cloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA()
    {
/*        try {
            Thread.sleep(1000);
            log.info("当前线程名称：" + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        log.info("当前线程名称：" + Thread.currentThread().getName());
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {
        return "------testB";
    }


    @GetMapping("/testD")
    public String testD(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("测试平均响应时间RT");
        return "测试平均响应时间RT";
    }

    @GetMapping("/testE")
    public String testE() {
        log.info("测试异常比例");
        int age = 10/0;
        return "测试异常比例";
    }

    @GetMapping("/testF")
    public String testF() {
        int age = 10/0;
        log.info("测试异常数");
        return "测试异常数";
    }

    @GetMapping("/testHostKey")
    @SentinelResource(value = "testHostKey",blockHandler = "Handler")
    public String testHostKey(@RequestParam(value = "p1",required = false) String p1
            ,@RequestParam(value = "p2",required = false) String p2){
        log.info("p1 = {}",p1);
        log.info("p2 = {}",p2);
        return "测试测试热点key";
    }

    public String Handler(@RequestParam(value = "p1",required = false) String p1
            , @RequestParam(value = "p2",required = false) String p2, BlockException exception){
        return "进入了blockHandler方法,异常信息：" + exception.getMessage();
    }
}
